// pages/songPackage/pages/searchdetail/searchdetail.js
import request from '../../../../utils/request'
// 获取全局实例
const appInstance = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keywords: '',
    first: true,
    songlist: [],
    imagelist: [],
    swiperList: [],
    length:0,
    searsongslist:[]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    

    // console.log(options)
    let keywords = options.keyword
    this.setData({
      keywords
    })

    this.getsearlist(keywords)
    //  this.towerSwiper('swiperList');
    let times = setInterval(()=>{
      let direction = this.data.direction;
      let list = this.data.swiperList;
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      //   console.log(list[list.length - 1].zIndex)
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    },2000)
  },

  async getsearlist(keywords) {
    let {length,searsongslist} = this.data
    let offset = length
    let idList = []
    console.log(offset);
    let res = await request('/cloudsearch?keywords', {
      keywords,
      limit:20,
      offset:offset
    })
    
    searsongslist.push(...res.result.songs)
    // console.log(searsongslist)
    searsongslist.forEach((item, index) => {
      let id = ''
      id = item.id
      idList.push(id)
    });
    // console.log(idList)
    length = idList.length
    let ids = idList.toString()
    // console.log(ids)
    this.setData({
      ids,
      searsongslist,
      length       //记录用户上期刷新音乐数目
    })
    this.getIdList() //获取歌单的音乐的列表
  },

  async getIdList() {
    let {
      ids,
      first
    } = this.data
    let res = await request('/song/detail', {
      ids
    });
    let res2 = await request('/song/url',{
      id:ids
    })
    // console.log(res.songs)
    // console.log(res2.data)
    let nourlids = []
    res2.data.forEach((item,index)=>{
      if(!item.url){
        nourlids.push(item.id)
        // console.log(item.id)
        // console.log(res.songs.indexOf(item.id)) 
        res.songs.forEach((items,indexs)=>{
          if(items.id === item.id){
            //  console.log(indexs)
            items.nourl=true
          }
        })
      }
    })
    // console.log(nourlids)
    let songlist = res.songs
    // console.log(songlist)
    this.setData({
      songlist
    })
    // console.log(songlist)
    if (first) {
      let imagelist = []
      songlist.forEach((item, index) => {
        let url = ''
        let id = ''
        url = item.al.picUrl
        id = item.id
        imagelist.push({
          url,
          id
        })
      });
      // console.log(imagelist)

      first = !first
      this.setData({
        first,
        imagelist
      })

      this.towerSwiper('imagelist');
    }

  },

  towerSwiper(name) {
    // console.log(this.data[name])
    let list = this.data[name];
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      swiperList: list
    })
  },

  towerStart(e) {
    // console.log("1")
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },
  // towerSwiper计算方向
  towerMove(e) {
    // console.log("2")
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })

  },
  // towerSwiper计算滚动
  towerEnd(e) {

    // console.log(console.log(this.data.direction))
    let direction = this.data.direction;
    let list = this.data.swiperList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      //   console.log(list[0].zIndex)
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      //   console.log(list[list.length - 1].zIndex)
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        swiperList: list
      })
    }
  },


  // 触底时间
  chudi(){
    console.log( '触底')
    let {keywords} = this.data
    this.getsearlist(keywords)
   

  },
    // 跳转至songDetail页面
    toSongDetail(event){
      console.log(event)
      if(!event.currentTarget.dataset.song.nourl){
        let {listId} = this.data
      let that = this;
      let globList=[];
      let index
      if(event.currentTarget.dataset.index){
         index = event.currentTarget.dataset.index;
      }else{
        index = 0
      }
      
      let {songlist} =this.data
      this.setData({
        index,
        type:`playList`
      })
      //  console.log(this.data.type)
      songlist.forEach((item,index) => {
          if(!item.nourl){
            let obj = {}
            obj.name = item.name;
            obj.artists= item.ar;
            obj.id = item.id
            globList.push(obj)
          }
        });
        // console.log(globList)
    
        this.setData({
          globList
        })
    
        
        appInstance.globalData.playList = that.data.globList;
        appInstance.globalData.currentIndex = index;
        // console.log(index)
        // console.log(songlist[index].id)
      // 路由跳转传参： query参数
      wx.navigateTo({
        // 不能直接将song对象作为参数传递，长度过长，会被自动截取掉
        // url: '/pages/songDetail/songDetail?songPackage=' + JSON.stringify(songPackage)
        url: `/pages/songPackage/pages/playmusic/playmusic?type=${this.data.type}&musicId=` + songlist[index].id,
        // success:function(){
  
        // }
      },
  
      )
      }else{
        wx.showToast({
          title:"暂时没有版权！",
          duration:2000,
          mask:true,
          icon:'none'
        })
      }
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})